/*
 * queryGuestRoomInfo.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.sun.org.apache.bcel.internal.generic.NEW;

import hotel.dao.BianJiDao;
import hotel.dao.KeFangDao;
import hotel.model.KeFang;
import hotel.util.DbUtil;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class queryGuestRoomInfo extends javax.swing.JInternalFrame {

	DbUtil dbUtil = new DbUtil();
	BianJiDao editModulDao = new BianJiDao();
	KeFangDao guestRoomDao = new KeFangDao();
	public queryGuestRoomInfo() {
		initComponents();

		String roomNum = null;
		this.fillTable(roomNum);
		this.setLocation(200, 50);//设置窗口位置
	}
	public void fillTable(String roomNum) {
		DefaultTableModel dtm = (DefaultTableModel) queryGuestRoomInfoTable
				.getModel();
		dtm.setRowCount(0);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet re = editModulDao.findGuestRoomState(con, roomNum);
			while (re.next()) {
				Vector v = new Vector();
				//添加每一行的数据
				v.add(re.getString("id"));
				v.add(re.getString("num"));
				v.add(re.getString("type"));
				String state = re.getString("state");
				if (state.equals("0")) {
					state = "未入住";
				} else {
					state = "入住中";
				}
				v.add(state);
				dtm.addRow(v);
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}
	}

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();

		jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));
		roomNum = new javax.swing.JTextField();
		search = new javax.swing.JButton();

		search.setFont(new Font("宋体", Font.PLAIN, 22));
		jScrollPane1 = new javax.swing.JScrollPane();
		queryGuestRoomInfoTable = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		jLabel2.setFont(new Font("宋体", Font.PLAIN, 22));
		jLabel3 = new javax.swing.JLabel();

		jLabel3.setFont(new Font("宋体", Font.PLAIN, 22));
		reRoomNum = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
	
		jLabel4.setFont(new Font("宋体", Font.PLAIN, 22));
		jLabel5 = new javax.swing.JLabel();
		jLabel5.setFont(new Font("宋体", Font.PLAIN, 22));

		state = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		jLabel6.setFont(new Font("宋体", Font.PLAIN, 22));
		type = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();

		jLabel7.setFont(new Font("宋体", Font.PLAIN, 22));
		id = new javax.swing.JTextField();
		editBtn = new javax.swing.JButton();

		editBtn.setFont(new Font("宋体", Font.PLAIN, 22));

		setClosable(true);
		setIconifiable(true);
		setTitle("查询客房信息");

		jLabel1.setText("房间号");

		search.setText("查询");
		search.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				searchActionPerformed(evt);
			}
		});

		queryGuestRoomInfoTable
				.setModel(new javax.swing.table.DefaultTableModel(
						new Object[][] { { null, null, null, null },
								{ null, null, null, null },
								{ null, null, null, null },
								{ null, null, null, null } }, new String[] {
								"编号", "房间号", "房间类型", "状态" }) {
					boolean[] canEdit = new boolean[] { false, false, false,
							false };

					public boolean isCellEditable(int rowIndex, int columnIndex) {
						return canEdit[columnIndex];
					}
				});
		queryGuestRoomInfoTable
				.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mousePressed(java.awt.event.MouseEvent evt) {
						queryGuestRoomInfoTableMousePressed(evt);
					}
				});
		jScrollPane1.setViewportView(queryGuestRoomInfoTable);

		jLabel2.setText("编辑客房信息");

		jLabel3.setText("房间号");

		reRoomNum.setEditable(false);

		jLabel4.setText("类型");

		jLabel5.setText("状态");

		jLabel6
				.setText("请填写未入住、入住中");

		jLabel7.setText("编号");

		id.setEditable(false);

		GroupLayout jPanel1Layout = new GroupLayout(
						jPanel1);
		jPanel1Layout.setHorizontalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
						.addComponent(jLabel2)
						.addGroup(jPanel1Layout.createSequentialGroup()
							.addGap(5)
							.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
								.addGroup(jPanel1Layout.createSequentialGroup()
									.addGap(10)
									.addComponent(jLabel5))
								.addComponent(jLabel3))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(state)
								.addComponent(reRoomNum, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
							.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
								.addGroup(jPanel1Layout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
									.addComponent(jLabel6)
									.addGap(86))
								.addGroup(jPanel1Layout.createSequentialGroup()
									.addGap(29)
									.addComponent(jLabel4)
									.addGap(18)
									.addComponent(type, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
									.addGap(75)
									.addComponent(jLabel7)
									.addGap(28)
									.addComponent(id, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)))))
					.addGap(85))
		);
		jPanel1Layout.setVerticalGroup(
			jPanel1Layout.createParallelGroup(Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jLabel2)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel3)
						.addComponent(reRoomNum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel4)
						.addComponent(type, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jLabel7)
						.addComponent(id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel5)
						.addComponent(jLabel6)
						.addComponent(state, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(35, Short.MAX_VALUE))
		);
		jPanel1.setLayout(jPanel1Layout);

		editBtn.setText("编辑");
		editBtn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				editBtnActionPerformed(evt);
			}
		});

		GroupLayout layout = new GroupLayout(
						getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(23)
					.addComponent(jLabel1)
					.addGap(18)
					.addComponent(roomNum, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(search)
					.addContainerGap())
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 1021, Short.MAX_VALUE))
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 891, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(178, Short.MAX_VALUE))
				.addGroup(layout.createSequentialGroup()
					.addGap(467)
					.addComponent(editBtn)
					.addContainerGap(504, Short.MAX_VALUE))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(35)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel1)
						.addComponent(roomNum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(search))
					.addPreferredGap(ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 125, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(23)
					.addComponent(editBtn))
		);
		getContentPane().setLayout(layout);

		pack();
	}
	private void searchActionPerformed(java.awt.event.ActionEvent evt) {
		String roomNum = this.roomNum.getText();
		this.fillTable(roomNum);
	}

	private void editBtnActionPerformed(java.awt.event.ActionEvent evt) {
		String roomNum = this.reRoomNum.getText();
		String type = this.type.getText();
		String state = this.state.getText();
		if ("".equals(roomNum) || roomNum == null) {
			JOptionPane.showMessageDialog(null, "房间号不能为空");
			return;
		}
		if ("".equals(type) || type == null) {
			JOptionPane.showMessageDialog(null, "类型不能为空");
			return;
		}
		if ("".equals(state) || state == null) {
			JOptionPane.showMessageDialog(null, "状态不能为空");
			return;
		}
		if ("入住中".equals(state)) {
			JOptionPane.showMessageDialog(null, "请输入正确的状态");
			return;
		}
		if ("未入住".equals(state) || "入住中".equals(state)) {
			int reState = 0;
			if("入住中".equals(state)){
				reState = 1;
			}
			
			KeFang guestRoom = new KeFang();
			guestRoom.setNum(Integer.parseInt(roomNum));
			guestRoom.setState(reState);
			guestRoom.setType(type);
			guestRoom.setId(Integer.parseInt(this.id.getText()));
			Connection con = null;
			try {
				con = dbUtil.getCon();
				int result = editModulDao.updateGuestRoom(con, guestRoom);
				if (result != 0) {
					JOptionPane.showMessageDialog(null, "修改成功");
					String num = null;
					this.fillTable(num);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "请输入正确的状态");
			return;
		}

	}
	private void queryGuestRoomInfoTableMousePressed(
			java.awt.event.MouseEvent evt) {
		//获取选择的行
		int row = queryGuestRoomInfoTable.getSelectedRow();
		this.reRoomNum.setText(queryGuestRoomInfoTable.getValueAt(row, 1)
				.toString());
		this.type
				.setText(queryGuestRoomInfoTable.getValueAt(row, 2).toString());
		this.state.setText(queryGuestRoomInfoTable.getValueAt(row, 3)
				.toString());
		this.id.setText(queryGuestRoomInfoTable.getValueAt(row, 0).toString());
	}

	private javax.swing.JButton editBtn;
	private javax.swing.JTextField id;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable queryGuestRoomInfoTable;
	private javax.swing.JTextField reRoomNum;
	private javax.swing.JTextField roomNum;
	private javax.swing.JButton search;
	private javax.swing.JTextField state;
	private javax.swing.JTextField type;


}